package com.tgy.twoDatabase.controller;

import com.alibaba.fastjson.JSON;
import com.tgy.twoDatabase.entity.Sms;
import com.tgy.twoDatabase.entity.Sendmessagelog;
import com.tgy.twoDatabase.entity.SentMessage;
import com.tgy.twoDatabase.service.MsmService;
import com.tgy.twoDatabase.service.SendmessagelogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * (sms.a_sms_wait_send)表控制层
 *
 * @author tangguoyi
 * @since 2021-09-27 14:35:31
 */
@RestController
@RequestMapping("sms")
@Slf4j
public class MsmController {
    /**
     * 服务对象
     */
    @Resource
    private MsmService msmService;
    @Resource
    private SendmessagelogService sendmessagelogService;



    @PostMapping("queryAllMessage")
    public Map<String, Object> queryAll(Sms sms) {
        return this.msmService.queryAll();
    }

    @PostMapping(value = "sendMessage")
    public Map<String,Object> sendMessage(@RequestBody String params) {
        //起始时间
        long start = System.currentTimeMillis();
        Map<String,Object> mapReturn = new HashMap<>();
        Sendmessagelog sendmessagelog = new Sendmessagelog();



        //向短信发送数据库发送数据库
        try{
            //获取前端信息
            Sms sms = JSON.parseObject(params, Sms.class);
            //向日志数据库发送日志文件
            sendmessagelog.setErrorMsg("success");
            sendmessagelog.setMsgcontext(sms.getMsg());
            sendmessagelog.setTelnum(sms.getSendTime());
            sendmessagelog.setSendtime(new Date());
            mapReturn =  this.msmService.insert(sms);
        } catch (Exception e) {
            log.error(String.valueOf(e));
            e.printStackTrace();
            sendmessagelog.setErrorMsg(e.getMessage());
        }
        finally {
            sendmessagelog.setReturnmsg(mapReturn.toString());
        }

        //执行发送
        this.sendmessagelogService.insert(sendmessagelog);

        //答应进程耗时
        System.out.println(String.format("任务执行成功,耗时{%s}", System.currentTimeMillis() - start));

        return mapReturn;
    }

    @Resource
    HttpServletRequest request;
    @PostMapping(value = "send")
    public Map<String,Object> send(@RequestBody String params) {
        log.info("访问ip"+(request.getRemoteAddr()));
        //起始时间
        long start = System.currentTimeMillis();


        Map<String,Object> mapReturn = new HashMap<>();


        //向短信发送数据库发送数据库
        Sms sms = JSON.parseObject(params, Sms.class);
        //向日志数据库发送日志文件
        Sendmessagelog sendmessagelog = new Sendmessagelog();
        sendmessagelog.setMsgcontext(sms.getMsg());
        sendmessagelog.setTelnum(sms.getSendTime());
        sendmessagelog.setSendtime(new Date());



        //向短信发送数据库发送数据库
        try{
            mapReturn =  this.msmService.insert(sms);
            log.info("发送短信服务成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("发送短信失败");
        }
        finally {
            sendmessagelog.setReturnmsg(mapReturn.toString());
        }

        //执行发送
        this.sendmessagelogService.insert(sendmessagelog);
        log.info("插入日志服务成功");

        //答应进程耗时
        log.info(String.format("任务执行成功,耗时{%s}", System.currentTimeMillis() - start));
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("时间",String.format("任务执行成功,耗时{%s}", System.currentTimeMillis() - start));
        return stringStringHashMap;

        //return mapReturn;
    }

    /*
    实体类入参，map出参查询已经发送的短信
     */
    @PostMapping("querySentMessage")
    public Map<String, Object> querySentMessage(@RequestBody SentMessage sentMessage) {
        try{
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String nowTime = df.format(new Date());
            System.out.println(nowTime);// new Date()为获取当前系统时间
            if(sentMessage.getEndTime()==null || sentMessage.getEndTime().equals("")){
                sentMessage.setEndTime(nowTime);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> map = new HashMap<>();
            map.put("errorMsg",e.getMessage());
        }
        return this.msmService.querySentMessage(sentMessage);
    }

/*
map入参，map出参查询已经发送的短信
 */
    @PostMapping("querySent")
    public Map<String, Object> querySentMessage(@RequestBody Map<String, String> params) {
        try{
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String nowTime = df.format(new Date());
            System.out.println(nowTime);// new Date()为获取当前系统时间
            if(params.get("endTime")==null || params.get("endTime").equals("")){
                params.put("endTime",nowTime);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> map = new HashMap<>();
            map.put("errorMsg",e.getMessage());
        }
        return this.msmService.querySent(params);
    }


    @PostMapping("queryById")
    public Map<String, Object> queryById(@RequestBody Sms sms) {
        return this.msmService.queryById(sms);
    }
}
